package com.pm.controller.dept;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.pm.common.Pages;
import com.pm.common.Response;
import com.pm.common.annotation.CurrentUser;
import com.pm.common.annotation.MyLog;
import com.pm.common.annotation.UserLoginToken;
import com.pm.common.exception.CommonException;
import com.pm.common.exception.ExpCodeState;
import com.pm.entity.po.dept.DepartmentEvaluate;
import com.pm.entity.po.security.User;
import com.pm.service.dept.DepartmentEvaluateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: pm_info_api
 * @description: 成员单位评价
 * @author: xzj
 * @create: 2020-08-19 11:20
 **/
@Api(tags = "成员单位评价")
@UserLoginToken
@RestController
@RequestMapping("/departmentEvaluate")
public class DepartmentEvaluateController {
    @Autowired
    private DepartmentEvaluateService departmentEvaluateService;

    @ApiOperation("新增接口")
    @PostMapping("/add")
    @ResponseBody
    @MyLog(value = "新增成员单位评价")
    @Transactional(rollbackFor = CommonException.class)
    public Response<Integer> add(@RequestBody JSONObject jsonObject, @CurrentUser User user) {
        Response<Integer> response = null;
        Integer result = departmentEvaluateService.addOne(jsonObject, user);
        if (result > 0) {
            response = new Response<>(ExpCodeState.add_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.add_data_failed);
        }
        return response;
    }

    @ApiOperation("单个对象查询接口")
    @PostMapping("/load")
    @ResponseBody
    public Response<DepartmentEvaluate> load(@RequestBody DepartmentEvaluate departmentEvaluate) {
        Response<DepartmentEvaluate> response = null;
        DepartmentEvaluate result = departmentEvaluateService.load(departmentEvaluate);
        response = new Response<DepartmentEvaluate>(ExpCodeState.query_data_success, result);
        return response;
    }

    @ApiOperation("全部列表查询接口")
    @PostMapping("/getList")
    @ResponseBody
    public Response<List<DepartmentEvaluate>> getList(@RequestBody DepartmentEvaluate departmentEvaluate) {
        List<DepartmentEvaluate> result = departmentEvaluateService.getList(departmentEvaluate);
        Response<List<DepartmentEvaluate>> res = new Response<List<DepartmentEvaluate>>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("全部查询")
    @PostMapping("/getListOne")
    @ResponseBody
    public Response<List<DepartmentEvaluate>> getListOne(@RequestBody DepartmentEvaluate departmentEvaluate) {
        Response<List<DepartmentEvaluate>> response = null;
        List<DepartmentEvaluate> result = departmentEvaluateService.getListOne(departmentEvaluate);
        response = new Response<List<DepartmentEvaluate>>(ExpCodeState.query_data_success, result);
        return response;
    }

    @ApiOperation("分页查询接口")
    @PostMapping("/getListByPage/{pageNum}/{pageSize}")
    @ResponseBody
    @MyLog(value = "查询任务信息")
    public Response<PageInfo<DepartmentEvaluate>> getList(DepartmentEvaluate departmentEvaluate, Pages page) {
        PageInfo<DepartmentEvaluate> result = departmentEvaluateService.getListByPage(departmentEvaluate, page);
        Response<PageInfo<DepartmentEvaluate>> res = new Response<PageInfo<DepartmentEvaluate>>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("删除接口")
    @PostMapping("/remove")
    @ResponseBody
    @MyLog(value = "删除部门信息")
    @Transactional(rollbackFor = CommonException.class)
    public Response<Integer> remove(DepartmentEvaluate departmentEvaluate) {
        Response<Integer> response = null;
        Integer result = departmentEvaluateService.remove(departmentEvaluate);
        if (result > 0) {
            response = new Response<>(ExpCodeState.remove_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.remove_data_failed);
        }
        return response;
    }

    @ApiOperation("删除接口")
    @PostMapping("/removeOne")
    @ResponseBody
    @MyLog(value = "删除部门信息")
    public Response<Integer> removeOne(Integer departmentId) {
        Response<Integer> response = null;
        Integer result = departmentEvaluateService.removeOne(departmentId);
        if (result > 0) {
            response = new Response<>(ExpCodeState.remove_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.remove_data_failed);
        }
        return response;
    }

    @ApiOperation("更新接口")
    @PostMapping("/update")
    @ResponseBody
    @MyLog(value = "编辑部门信息")
    public Response<Integer> updateDepartmentEvaluate(@RequestBody DepartmentEvaluate departmentEvaluate) {
        // 删除
        Response<Integer> response = null;
        Integer result = departmentEvaluateService.updateDepartmentEvaluate(departmentEvaluate);
        if (result > 0) {
            response = new Response<>(ExpCodeState.update_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.update_data_failed);
        }
        return response;
    }
}
